package com.chief.hadoop.mr.join.reduce;

import com.chief.hadoop.mr.join.OrderBean;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class OrderMapper extends Mapper<LongWritable, Text, Text, OrderBean> {

    String fileName;
    Text outKey = new Text();
    OrderBean outVal = new OrderBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split(" ");
        if (fileName.contains("order")) {
            outKey.set(split[1]);

            outVal.setOrderId(split[0]);
            outVal.setGoodsId(split[1]);
            outVal.setCnt(Integer.valueOf(split[2]));
        } else {
            outKey.set(split[0]);
            outVal.setGoodsId(split[0]);
            outVal.setGoodsName(split[1]);
        }
        context.write(outKey, outVal);
    }

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        InputSplit inputSplit = context.getInputSplit();
        FileSplit fileSplit = (FileSplit) inputSplit;
        fileName = fileSplit.getPath().getName();
    }
}
